package com.cxdr.awsips.common.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.cxdr.awsips.common.bean.Station;
import com.cxdr.awsips.common.db.DBHelper;
import com.cxdr.awsips.common.logger.LogFactory;

public class StationDao {
	private static Logger logger = LogFactory.makeNewLogger(StationDao.class);
	private List<Station> all = new ArrayList<Station>();
	private Connection connection ;
	private static StationDao instance =new StationDao();
	private StationDao(){
		try {
			connection = DBHelper.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static List<Station> getAll() {
		return getAll(false);
	}
	
	public static List<Station> getAll(boolean reload) {
		if(reload){
			try{
				Statement stmt = instance.connection.createStatement();
				ResultSet rs =  stmt.executeQuery("select * from t_station");
				instance.all = new ArrayList<Station>();
				while(rs.next()){
					Station bean = new Station();
					bean.setArea_id(rs.getString("area_id"));
					bean.setDevice_type(rs.getString("device_type"));
					bean.setElevation(rs.getDouble("elevation"));// (rs.getString("area_id"));
					bean.setId(rs.getString("id"));
					bean.setLatitude(rs.getString("latitude"));
					bean.setLongitude(rs.getString("longitude"));
					bean.setRemarks(rs.getString("remarks"));
					bean.setStation_name(rs.getString("station_name"));
					bean.setStation_type(rs.getString("station_type"));
					bean.setSummary(rs.getString("summary"));
					bean.setTelephone(rs.getString("telephone"));
					instance.all.add(bean);
				}
			}catch(SQLException sqle){
				logger.error("",sqle);
				sqle.printStackTrace();
			}
		}
		return instance.all;
	}
	public static void main(String args[]){
		List<Station> list = StationDao.getAll(true);
		for(Station s:list){
			System.out.println(s.getStation_name());
		}
	}
}
